package com.piece.mall.context;

import com.piece.core.framework.enums.StatusType;
import com.piece.core.framework.util.basic.SpringUtil;
import com.piece.core.framework.util.schedule.ScheduleJob;
import com.piece.core.framework.util.schedule.ScheduleUtil;
import com.piece.core.web.initializer.SystemScript;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 开启秒杀商品的定时上架
 */
@Slf4j
@Component
public class InitSeckillTimeTask implements SystemScript {

    @Autowired
    private Scheduler scheduler;

    @Override
    public String getScriptName() {
        return "加载定时器任务";
    }

    @Override
    public void doInit() {
        // 加载定时器
        ScheduleUtil.createScheduleJob(scheduler, transToScheduleJob());
    }

    private ScheduleJob transToScheduleJob() {
        ScheduleJob scheduleJob = new ScheduleJob("job_mall_seckill_upload", "秒杀商品定时上架", "0 0 0/6 * * ?", "1");
        String className = "seckillService";
        String methodName = "uploadSeckillSkuLatest3Days";
        scheduleJob.setTarget(SpringUtil.getBean(className));
        scheduleJob.setMethod(methodName);
        scheduleJob.setStatus(StatusType.RUN.getCode());
        scheduleJob.setRetryCount(3);
        scheduleJob.setRetryInterval(3000);
        return scheduleJob;
    }

    @Override
    public Integer getIndex() {
        return Integer.MAX_VALUE;
    }
}
